package com.example.ftp;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FTPFileUpload {

    public static void main(String[] args) {
        String server = "10.11.10.45";
        int port = 8083;
        String user = "hdxmgl";
        String password = "1qaz@WSX,";
        String filePath = "D://test.docx";
        String remoteDir = "/";
        String fileName = "test.docx";

        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(server, port);
            ftpClient.login(user, password);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            File localFile = new File(filePath);
            FileInputStream inputStream = new FileInputStream(localFile);

            boolean done = ftpClient.storeFile(remoteDir + "/" + fileName, inputStream);
            inputStream.close();
            if (done) {
                System.out.println("The file has been uploaded successfully.");
            } else {
                System.out.println("Failed to upload the file.");
            }
        } catch (IOException ex) {
            System.out.println("Error: " + ex.getMessage());
            ex.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
